/**
 * Created by Administrator on 2017/5/8.
 */

var express = require('express');
var router = express.Router();

var getHmUrlDao = require('../dao/getHmUrl.js');
var getHmUrlRouter = module.exports;

function paramAll(req){
    var parameter = {};
    if(req.params) {
        for(var i in req.params) {
            parameter[i] = req.params[i];
        }
    }
    if(req.body){
        for(var i in req.body) {
            parameter[i] = req.body[i];
        }
    }
    if(req.query){
        for(var i in req.query) {
            parameter[i] = req.query[i];
        }
    }
    return parameter;
}

getHmUrlRouter.getUrl = function(req, res) {
    var par = paramAll(req);
    if(!par.id) {
        res.json(new ERR('地址不正确,请重新打开', 400));
        return;
    }
    var arr = par.id.split('_');
    if(arr.length == 2) {
        par.uid = arr[0];
        par.hid = arr[1];
    } else {
        res.json(new ERR('地址不正确,请重新打开', 400));
        return;
    }

    getHmUrlDao.getUrl(par, function (e, d) {
        if(e) {
            console.log(e);
            res.json(new ERR('获取链接失败！', 400));
        } else {
            var img_url = d[0];
            res.render('qrcode', {title:'长按识别二维码进群', img_url:img_url});
        }
    });
}
router.get('/:id', getHmUrlRouter.getUrl);

module.exports = router;